package com.usian.controller;

import com.usian.pojo.*;
import com.usian.service.DeptService;
import com.usian.service.EmpService;
import com.usian.service.ZhiService;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("emp")
public class EmpController {
    @Autowired
    private EmpService empService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private ZhiService zhiService;

    @RequestMapping("login")
    public String tologin(Role role, HttpSession session) {
       Role role1 = empService.findByName(role);
    /*   session.setAttribute("rid",role1.getRid());出错了*/
        if (role1 != null) {
            return "show";
        }
        return "login";
    }

    //addDept
    @RequestMapping("addDept")
    public String addDept(Dept dept) {
        deptService.addDept(dept);
        return "redirect:/t/todept";
    }
    //deleteDept
    @RequestMapping("deleteDept")
    public String deleteDept(Integer id) {
        deptService.deleteDept(id);
        return "redirect:/t/todept";
    }
    //toUpDept
    @RequestMapping("toUpDept")
    public String toUpDept(Integer id, Model model) {
      Dept dept= deptService.findByDept(id);
      model.addAttribute("dept",dept);
        return "updateDept";
    }
    //upDept
    @RequestMapping("upDept")
    public String upDept(Dept dept) {
        deptService.upDept(dept);
        return "redirect:/t/todept";
    }
    //toaddzhi
    @RequestMapping("toaddzhi")
    public String toAddZhi(Model model) {
        List<Dept> list= deptService.findByDepts();
        model.addAttribute("dept",list);
        return "addzhi";
    }
    //addZhi
    @RequestMapping("addZhi")
    public String addZhi(Zhi zhi) {
        zhiService.addZhi(zhi);
        return "redirect:/t/tozhi";
    }
    //deleteZhi
    @RequestMapping("deleteZhi")
    public String deleteZhi(Integer id) {
        zhiService.deleteZhi(id);
        return "redirect:/t/tozhi";
    }
    //toupZhi
    @RequestMapping("toupZhi")
    public String toupZhi(Integer id, Model model) {
        Zd zd= zhiService.findByid(id);
        model.addAttribute("zd",zd);
        return "updateZhi";
    }
    //upZhi
    @RequestMapping("upZhi")
    public String upZhi(Zd zd) {
        zhiService.upZhi(zd);
        return "redirect:/t/tozhi";
    }
    //deleteEmp
    @RequestMapping("deleteEmp")
    public String deleteEmp(Integer id) {
        empService.deleteEmp(id);
        return "redirect:/t/toemp";
    }
    //toupEmp
    @RequestMapping("toupEmp")
    public String toupEmp(Integer id, Model model) {
        Emp emp= empService.findByid(id);
        List<Zhi> zhi=zhiService.findZhi1();
        List<Dept> depts=deptService.findByDepts();
        model.addAttribute("emp",emp);
        model.addAttribute("zhi",zhi);
        model.addAttribute("dept",depts);
        return "updateEmp";
    }
    //upemp
    @RequestMapping("upemp")
    public String upZhi(Emp emp) {
        empService.upemp(emp);
        return "redirect:/t/toemp";
    }
    //addemp
    @RequestMapping("addemp")
    public String toaddemp(Emp emp) {
        empService.addemp(emp);
        return "redirect:/t/toemp";
    }
    //toaddemp
    @RequestMapping("toaddemp")
    public String toaddemp( Model model) {
        List<Zhi> zhi=zhiService.findZhi1();
        List<Dept> depts=deptService.findByDepts();
        model.addAttribute("zhi",zhi);
        model.addAttribute("dept",depts);
        return "addEmp";
    }
    //首页
    @RequestMapping("to")
    public String to() {

        return "show";
    }
    //退出
    @RequestMapping("tui")
    public String tui() {

        return "login";
    }

}
